//
//  SingerViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-5-22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SingerViewController.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "SingerModel.h"
#import "SingerTableViewCell.h"
#import "SingerDetailViewController.h"

@interface SingerViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain) NSMutableArray * array;
@property(nonatomic, retain) UICollectionView * collectView;

@end

@implementation SingerViewController
- (void)dealloc
{
    [_array release];
    [super dealloc];
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        
    }
    return self;
}
- (void)loadView
{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor clearColor]];

    [self SongerCollectionView];
    
    [SANetWorkingTask request:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=826886cd55c251bacd39b2d6523c12b126f3c590&idfa=27D84D88-B1C6-4173-B7C5-EE78CD26E7FB&utdid=VVrl99FV5JUDAA1STsAtAkrx&alf=201200&bundle_id=com.ttpod.music" parmater:nil block:^(NSDictionary *result) {
        
        NSArray * arrayTable = [result objectForKey:@"data"];
        for (NSMutableDictionary * dicData in arrayTable) {
            SingerModel * singerModel = [[SingerModel alloc]init];
            [singerModel setValuesForKeysWithDictionary:dicData];
            [self.array addObject:singerModel];

        }
        
        [_collectView reloadData];
        
    }];
    
}

#pragma mark -
#pragma mark 创建歌手collectionView

- (void)SongerCollectionView
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //    上下之间的间隔
    layout.minimumLineSpacing =self.view.frame.size.height/667.0 *20;
    //    左右之间的间隔
    layout.minimumInteritemSpacing = self.view.frame.size.width / 375.0* 20;
    layout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width/375.0* 15,self.view.frame.size.height/667.0 *15,self.view.frame.size.width/375.0*15,self.view.frame.size.height/667.0*15);
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 375.0 * 100, self.view.frame.size.height / 667.0 * 130);
    
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height  - self.view.frame.size.height / 667.0 * 64 - self.view.frame.size.height / 667.0 * 80 ) collectionViewLayout:layout];
    
    [self.collectView registerClass:[SingerTableViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;

    [self.collectView setBackgroundColor:[UIColor clearColor]];

    [self.view addSubview:self.collectView];
    [self.collectView release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SingerTableViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    SingerModel * model  = [self.array objectAtIndex:indexPath.row];
    cell.label.text = model.title;
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil];
    
    return cell;
    
}

//传值点击进入下一页
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SingerModel * singer = [self.array objectAtIndex:indexPath.row];
    SingerDetailViewController * detail = [[SingerDetailViewController alloc] init];
    detail.singerId = singer.id;
    detail.aTitle = singer.title;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}


@end
